package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional//事物控制
public class SetmealServiceImpl implements SetmealService {

    //依赖注入
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private DishMapper dishMapper;

    //查询所有
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //根据姓名模糊查询
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        wrapper.like(StringUtils.isNoneEmpty(name), Setmeal::getName, name);
        //设置分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //执行分页查询
        page = setmealMapper.selectPage(page, wrapper);
        System.out.println(page);
        //遍历分页结果，根据套餐的分类名获取菜品列表
        List<Setmeal> setmealList = page.getRecords();//查询当前页信息
        //非空判断
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据id获取套餐分类名称（已开发过的功能）
                Category category = categoryMapper.selectById(setmeal.getId());
                //查询套餐下菜品信息
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                //设置查询条件(where setmeal_id = #{id})
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                //使用setMealDishMapper查询,查询到的是多条数据
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                //存储到setMeal表中的字段里
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }

    //添加套餐数据
    @Override
    public void save(Setmeal setmeal) {
        //基本信息直接保存
        setmealMapper.insert(setmeal);

        //菜单分类信息保存到中间表中
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //存储setmealDish数据
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //删除套餐数据
    @Override
    public void deleteById(Long[] ids) {

        //确认被删除的数据都是在售状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //构建查询条件(where id = #{id} in status = 1)
        wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        Integer number = setmealMapper.selectCount(wrapper);

        //判断number
        if (number > 0) {
            throw new CustomException("当前商品处于在售状态，无法删除");
        }

        //执行删除
        //先删除中间表中数据（包括分类，套餐表中的菜品）
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            //构建查询条件
            wrapper1.eq(SetmealDish::getSetmealId, id);
            //执行删除
            setmealDishMapper.delete(wrapper1);

            //删除套餐表中数据
            setmealMapper.deleteById(id);
        }


    }

    //修改状态
    @Override
    public void updateStatus(Integer status, List<Long> ids) {

        //先获取套餐数据
        List<Setmeal> setmealList = setmealMapper.selectBatchIds(ids);
        //非空判断
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                setmeal.setStatus(status);
                //修改数据
                setmealMapper.updateById(setmeal);

            }
        }

    }

    //修改前回显套餐数据
    @Override
    public Setmeal updateById(long id) {

        //根据id查询套餐数据
        Setmeal setmeal = setmealMapper.selectById(id);

        //再根据中间表查询菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);

        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }

    //修改数据
    @Override
    public void update(Setmeal setmeal) {
        //先修改setmeal表中数据
        setmealMapper.updateById(setmeal);

        //先删除中间表中数据，然后重新添加
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        //构建删除条件，根据id删除
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //重新添加数据(先获取套餐表中菜品集合)
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        //非空判断
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //存储setmealDish数据
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //根据categoryid查询套餐数据
    @Override
    public List<Setmeal> findByCategory(Long categoryId, Integer status) {

        //根据categoryId查询套餐信息
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);

        //查询
        return setmealMapper.selectList(wrapper);
    }


    //点击套餐是显示套餐下具体菜品详情
    @Override
    public List<Dish> findById(long id) {
        //构建查询条件
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        //from setmeal_dish where setmealId = #{id}
        wrapper.eq(SetmealDish::getSetmealId,id);
        //查询
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //创建一个集合，用于存储dish菜品数据
        List<Dish> dishList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                //根据dish_id查询数据
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                //设置份数
                dish.setCopies(setmealDish.getCopies());
                dishList.add(dish);
            }
        }
        return dishList;
    }
}


